<?php
include "config.php";
$querys = "truncate table jarak";
$adds = mysql_query($querys);
$x =1;
$results = mysql_query("SELECT count(*) FROM `transport`");
$rows = mysql_fetch_row($results);
$nos = $rows[0];

for ($i = 1; $i <= $nos; $i++) {
	//echo $i ."<br>";
	$result = mysql_query("SELECT count(*) FROM `jalur` where idt =$i");
	$row = mysql_fetch_row($result);
	$no = $row[0];
//echo $no."<br>";
	for ($j = 1; $j < $no; $j++) {
	$result1 = mysql_query("SELECT id,lat,lng FROM `jalur`where id =$x");
	$row1 = mysql_fetch_row($result1);
	$id1 = $row1[0];
	$lat1 = $row1[1];
	$lng1 = $row1[2];
	
	$x = $x+1;
	//echo $x;
	$result2 = mysql_query("SELECT id, lat, lng FROM `jalur`where id =$x");
	$row2 = mysql_fetch_row($result2);
	$id2 = $row2[0];
	$lat2 = $row2[1];
	$lng2 = $row2[2];
	//echo $id2."=".$lat2."dan".$lng2."<br>";
	
	$longitude1 = $lng1;
	$longitude2 = $lng2;
	$latitude1 = $lat1;
	$latitude2 = $lat2;
	$unit = 'Mi';

	$theta = $longitude1 - $longitude2;
	$distance = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) +
	(cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) *
	cos(deg2rad($theta)));
	$distance = acos($distance);
	$distance = rad2deg($distance);
	$distance = $distance * 60 * 1.1515;
	$distances = $distance * 1609.344;

	//echo $id1."dan".$id2."=".$distance."<br>";
	$sql= "INSERT INTO jarak (id1, id2, jarak) VALUES ($id1, $id2, $distances)";
	$add = mysql_query($sql);
	}
	$x++;
}
?>